-- ==== BEGIN LICENSE BLOCK ====
-- storyboard-addon - Copyright (C) Arnaud Charnoz, 2006-2010.
-- Distributed under the terms of the GNU Lesser General Public License (LGPL) as
-- published by the Free Software Foundation.
-- ===== END LICENSE BLOCK ====

--===========================================================================--
--===========================================================================--
-- [ FOCNCTIONS ] --
--===========================================================================--
--===========================================================================--


--===========================================================================--
-- [ FONCTION DE CHARGEMENT] --
--===========================================================================--

-- [ Managment of event] --
function storyboard_OnEvent()
   storyboard_debugMessage("storyboard_OnEvent");
   if ( event == "VARIABLES_LOADED" ) then
      storyboard_VARIABLES_LOADED();
   end
end

-- [ Onload function ] --
function storyboard_onLoad()
  
  storyboard_debugMessage("storyboard_onLoad");
  storyboard_sendAlertMessage(STORYBOARD_MES_CONSOLEINFO_001,1);
  storyboard_onLoadNbElement();
  storyboard_resetVisibiltyOfObject();
  this:RegisterEvent("VARIABLES_LOADED");
  
end


-- [ Loaded Variable ] --
function storyboard_VARIABLES_LOADED()
        
        storyboard_debugMessage("storyboard_VARIABLES_LOADED");
        
    if ( not STORYBOARD_SAVED ) then
       STORYBOARD_SAVED = {}
    end
    
    if ( not STORYBOARD_SAVED.STORYBOARD_VERSION ) then
       STORYBOARD_SAVED.STORYBOARD_VERSION = 0;
    end
    
    if ( STORYBOARD_SAVED.STORYBOARD_VERSION < 940 ) then
       STORYBOARD_SAVED.INFO_SAY_MODE = true;
       STORYBOARD_SAVED.INFO_ANSWER_MODE = true;
       STORYBOARD_SAVED.INFO_OBJECT_MODE = true;
    end
    
    STORYBOARD_SAVED.STORYBOARD_VERSION = STORYBOARD_VERSION
    
    if ( not STORYBOARD_SAVED.POSITION ) then
      STORYBOARD_SAVED.POSITION = 30;
    end
    SbQuestIcon:SetPoint("TOPLEFT", "Minimap", "TOPLEFT", 52 - (80 * cos(STORYBOARD_SAVED.POSITION)), (80 * sin(STORYBOARD_SAVED.POSITION)) - 52);
    
       if ( not STORYBOARD_SAVED.SCENAR_VERSION ) then 
         STORYBOARD_SAVED.SCENAR_VERSION = 0; 
    end
    
    if ( not STORYBOARD_SAVED.SCENAR_TITLE ) then 
         STORYBOARD_SAVED.SCENAR_TITLE = " "; 
    end
    
    storyboard_debugMessage(STORYBOARD_SAVED.SCENAR_VERSION);
    storyboard_debugMessage(STORYBOARD_SAVED.SCENAR_TITLE);
    
    if ( STORYBOARD_SAVED.SCENAR_VERSION <  STORYBOARD_SCENARIO.VERSION_NUMERIC or
         STORYBOARD_SAVED.SCENAR_TITLE  ~=  STORYBOARD_SCENARIO.TITLE ) then 
        storyboard_sendAlertMessage(STORYBOARD_MES_CONSOLEINFO_004,1);
        STORYBOARD_SAVED.SCENAR_VERSION = STORYBOARD_SCENARIO.VERSION_NUMERIC;
        STORYBOARD_SAVED.SCENAR_TITLE = STORYBOARD_SCENARIO.TITLE
        storyboard_resetVisibiltyOfObject();
    end
    
    storyboard_debugMessage(STORYBOARD_SAVED.SCENAR_VERSION);
    storyboard_debugMessage(STORYBOARD_SAVED.SCENAR_TITLE);
end




-- [ Met a jour le nombre d'�l�ments des listes d'objets, cibles et lieux] --
function storyboard_onLoadNbElement()
  storyboard_debugMessage("storyboard_onLoadNbElement"); 
  
  local cmpt = 1
  while true do
  local test = STORYBOARD_SCENARIO_TARGET[cmpt]
  if test == nil then break end
  cmpt = cmpt + 1
  end
  STORYBOARD_FS_NB_TARGET = cmpt - 1

  cmpt = 1
  while true do
  local test = STORYBOARD_SCENARIO_LOCATION[cmpt]
  if test == nil then break end
  cmpt = cmpt + 1
  end
  STORYBOARD_FS_NB_LOCATION = cmpt - 1
  
  cmpt = 1
  while true do
  local test = STORYBOARD_SCENARIO_OBJECT[cmpt]
  if test == nil then break end
  cmpt = cmpt + 1
  end
  STORYBOARD_FS_NB_OBJECT = cmpt - 1
  
end


-- [ Met a jour la visibil� des objets ] --
function storyboard_resetVisibiltyOfObject()
   for idObject = 1, STORYBOARD_FS_NB_OBJECT, 1 do
     local id = STORYBOARD_SCENARIO_OBJECT[idObject].id;
       STORYBOARD_SAVED_OBJECT[id]=false;     
   end
end


-- [ Chargement du scenario] --
function storyboard_showQuestFrame()

   if UnitExists("target") and (STORYBOARD_FS_EVENT == STORYBOARD_FS_EVENT_SPEAK_WITH_TARGET or STORYBOARD_FS_EVENT == STORYBOARD_FS_EVENT_EXPLORE_TARGET )then
   SetPortraitTexture(SbQuestFramePortrait, "target");
   else
   SbQuestFramePortrait:SetTexture("Interface\\QuestFrame\\UI-QuestLog-BookIcon");
   end
    
   STORYBOARD_NPCSAY = 1
   SbQuestFrame:SetAlpha(1)
   SbQuestFrame:Show()
end


